package com.android.kotlincoroutinemvvmdemo

import android.os.Bundle
import android.util.Log
import android.view.View
import com.android.kotlincoroutinemvvmdemo.base.BaseActivity
import com.android.kotlincoroutinemvvmdemo.contentprovider.GestureDBAction
import com.android.kotlincoroutinemvvmdemo.contentprovider.StudentDBAction
import com.android.kotlincoroutinemvvmdemo.databinding.ActivityContentProviderBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ContentProviderActivity : BaseActivity<ActivityContentProviderBinding>(), View.OnClickListener {

    override fun getLayoutId() = R.layout.activity_content_provider

    override fun init(savedInstanceState: Bundle?) {

        mBinding.btnInsert.setOnClickListener(this)
        mBinding.btnQuery.setOnClickListener(this)
        mBinding.btnUpdate0.setOnClickListener(this)
        mBinding.btnUpdate1.setOnClickListener(this)
        mBinding.btnDelete.setOnClickListener(this)

        mBinding.btnInsert1.setOnClickListener(this)
        mBinding.btnQuery1.setOnClickListener(this)

    }

    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.btn_insert -> {
                val switch = (mBinding.number0.text.toString()).toInt()
                val result = (mBinding.number1.text.toString()).toInt()
                val desc = (mBinding.number2.text.toString())
                launch {
                    withContext(Dispatchers.IO) {
                        //GestureDBAction.insertValue(contentResolver, switch, result)
                        GestureDBAction.insertValue(contentResolver, switch, result, desc)
                    }
                }
            }
            R.id.btn_query -> {
                launch {
                    val resultList = withContext(Dispatchers.IO) {
                        GestureDBAction.queryValue(contentResolver)
                    }
                    for (item in resultList) {
                        Log.d(TAG, "id:${item.id},switch:${item.switch},result:${item.result},desc:${item.desc}")
                    }
                }
            }
            R.id.btn_update0 -> {
                val switch = (mBinding.number0.text.toString()).toInt()
                launch {
                    withContext(Dispatchers.IO) {
                        GestureDBAction.updateValue0(contentResolver, switch)
                    }
                }
            }
            R.id.btn_update1 -> {
                val result = (mBinding.number1.text.toString()).toInt()
                launch {
                    withContext(Dispatchers.IO) {
                        GestureDBAction.updateValue1(contentResolver, result)
                    }
                }

            }
            R.id.btn_delete -> {


            }

            R.id.btn_insert1 -> {

                launch {
                    StudentDBAction.insertValue(
                        contentResolver,
                        1102,
                        "resea",
                        "三年级",
                        "8562323",
                        "游泳"
                        )

                    StudentDBAction.insertValue(
                        contentResolver,
                        1109,
                        "hndeqe",
                        "一年级",
                        "30265651",
                        "跑步"
                        )
                }
            }

            R.id.btn_query1 -> {
                launch {
                    val resultList = withContext(Dispatchers.IO) {
                        StudentDBAction.queryValue(contentResolver)
                    }
                    for (item in resultList) {
                        Log.d(
                            TAG,
                            "numberId:${item.numberId},name:${item.name},grade:${item.grade},phone:${item.phone},hobby:${item.hobby}"
                        )
                    }
                }
            }
        }
    }

    companion object {
        const val TAG = "ContentProviderActivity"
    }
}